package com.hanscan.gui;

import com.hanscan.persistence.controllers.UserJpaController;
import com.hanscan.persistence.controllers.exceptions.NonexistentEntityException;
import com.hanscan.persistence.entities.User;
import com.hanscan.util.language.LanguageConfig;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlCommandLink;
import com.icesoft.faces.component.ext.HtmlInputSecret;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputLabel;
import com.icesoft.faces.component.ext.HtmlPanelGroup;
import com.icesoft.faces.component.jsfcl.data.PopupBean;
import com.icesoft.faces.component.panelstack.PanelStack;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import java.beans.XMLEncoder;
import java.io.ByteArrayOutputStream;
import javax.faces.FacesException;
import javax.faces.event.ActionEvent;


public class Index extends AbstractPageBean
{
    //
    //Cambiar contraseña
    //
    private PanelStack panelStackLogin = new PanelStack();
    private HtmlOutputLabel labelTitleChangePassword = new HtmlOutputLabel();
    private HtmlPanelGroup panelChangePassword = new HtmlPanelGroup();
    private HtmlOutputLabel outputLabelChangePasswordUserName =
            new HtmlOutputLabel();
    private HtmlOutputLabel outputLabelChangePasswordOld =
            new HtmlOutputLabel();
    private HtmlOutputLabel outputLabelChangePasswordNew =
            new HtmlOutputLabel();
    private HtmlOutputLabel outputLabelChangePasswordVerify =
            new HtmlOutputLabel();
    private HtmlInputText inputTextChangePasswordUserName = new HtmlInputText();
    private HtmlInputSecret inputTextChangePasswordOld = new HtmlInputSecret();
    private HtmlInputSecret inputTextChangePasswordNew = new HtmlInputSecret();
    private HtmlInputSecret inputTextChangePasswordVerify =
            new HtmlInputSecret();
    private HtmlCommandButton buttonChangePassword = new HtmlCommandButton();
    private HtmlCommandLink commandLinkChangePassword = new HtmlCommandLink();
    private HtmlCommandLink commandLinkChangePasswordBack =
            new HtmlCommandLink();

    //
    //Login
    //
    private HtmlPanelGroup panelLogin = new HtmlPanelGroup();
    private HtmlOutputLabel outputLabelUserLogin = new HtmlOutputLabel();
    private HtmlOutputLabel outputLabelUserPassword = new HtmlOutputLabel();
    private HtmlInputSecret inputSecretUserPassword = new HtmlInputSecret();
    private HtmlInputText inputTextUserLogin = new HtmlInputText();
    private HtmlCommandButton buttonLogin = new HtmlCommandButton();

    //
    //Pop Up
    //
    private PopupBean panelPopupPruebaBean = new PopupBean();
    private String messageText = new String();
    private String messageHeader = new String();
    private HtmlCommandButton btnClosePopUp = new HtmlCommandButton();


    //IDIOMAS
    //es --> español y en --> inglés //IDIOMA POR DEFECTO
    private String idioma;
    final String LANGUAGE_ES = "es";
    final String COUNTRY_ES = "ES";
    final String LANGUAGE_EN = "en";
    final String COUNTRY_US = "US";


    private void _init() throws Exception
    {
        panelPopupPruebaBean.setShowModalPanel(false);
        idioma = LANGUAGE_ES;

        if (idioma.equals(LANGUAGE_ES))
        {
            getSessionBean1().getSessionBeanLanguage().changeLanguage(LANGUAGE_ES, COUNTRY_ES);
        }
        else if (idioma.equals(LANGUAGE_EN))
        {
            getSessionBean1().getSessionBeanLanguage().changeLanguage(LANGUAGE_EN, COUNTRY_US);
        }
        loadLanguage(getSessionBean1().getSessionBeanLanguage());
    }

    public Index()
    {
    }

    //carga los textos guardados en los properties
    protected void loadLanguage(LanguageConfig languageConfig)
    {
        outputLabelUserLogin.setValue(languageConfig.getProperty("Usuario"));
        outputLabelUserPassword.setValue(languageConfig.getProperty("Password"));
        buttonLogin.setValue(languageConfig.getProperty("Login"));

        btnClosePopUp.setValue(languageConfig.getProperty("Cerrar"));
        labelTitleChangePassword.setValue(languageConfig.getProperty(
                "Cambiar_Contraseña"));
        outputLabelChangePasswordUserName.setValue(languageConfig.getProperty(
                "Usuario"));
        outputLabelChangePasswordOld.setValue(languageConfig.getProperty(
                "Old_Password"));
        outputLabelChangePasswordNew.setValue(languageConfig.getProperty(
                "New_Password"));
        outputLabelChangePasswordVerify.setValue(languageConfig.getProperty(
                "Confirm_Password"));
        commandLinkChangePassword.setValue(languageConfig.getProperty(
                "Cambiar_Contraseña"));
        buttonChangePassword.setValue(languageConfig.getProperty(
                "Cambiar_Contraseña"));
        commandLinkChangePasswordBack.setValue(languageConfig.getProperty(
                "Volver"));

    }

    //codifica cualquier objeto a XML
    public String encodeXML(Object objeto)
    {
        try
        {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            XMLEncoder e = new XMLEncoder(bout);
            e.writeObject(objeto);
            e.close();
            String s = bout.toString();
            System.out.println("Salida:" + s);

            //SUSTITUIR LOS \N POR ' '
            String cadrep = s.replace('\n', ' ');

            return cadrep;
        }
        catch (Exception ex)
        {
            System.out.println("error");
            return "error";
        }
    }
    
    public void commandLinkChangePasswordBack_processAction(ActionEvent ae)
    {
        panelStackLogin.setSelectedPanel("panelLogin");
    }

    //
    //Cambiar contraseña
    public String buttonChangePassword_action(ActionEvent actionEvent)
    {
        if(inputTextChangePasswordNew.getValue().toString().equals(inputTextChangePasswordVerify.getValue().toString()))
        {
            try
            {
                UserJpaController userJpaController = new UserJpaController();

                User user = userJpaController.findClientByUserNameAndPassword
                   (inputTextUserLogin.getValue().toString(), inputSecretUserPassword.getValue().toString());
                user.setClientPassword(inputTextChangePasswordNew.getValue().toString());
                userJpaController.edit(user);
                return null;
            }
           catch (NonexistentEntityException ex)
            {
                panelPopupPruebaBean.setShowModalPanel(true);
                messageHeader = "Error";
                messageText = "Usuario o contraseña incorrecta";
                ex.printStackTrace();
                return null;
            }
            catch (Exception ex)
            {
                panelPopupPruebaBean.setShowModalPanel(true);
                messageHeader = "Error";
                messageText = "Error en la conexion de base de datos";
                ex.printStackTrace();
                return null;
            }
        }
        else
        {
            panelPopupPruebaBean.setShowModalPanel(true);
            messageHeader = "Error";
            messageText = "No son iguales la nueva contraseña";
            return null;
        }

    }

    public void commandLinkChangePassword_processAction(ActionEvent ae)
    {
        inputTextChangePasswordNew.resetValue();
        inputTextChangePasswordOld.resetValue();
        inputTextChangePasswordVerify.resetValue();
        inputTextChangePasswordUserName.resetValue();
        panelStackLogin.setSelectedPanel("panelChangePassword");
    }

    /**
     * Comprobar usuario registrado por nombre de usuario y contraseña
     * @return
     */
    public String buttonLogin_action()
    {
        try
        {
            UserJpaController userJpaController = new UserJpaController();
            User user = userJpaController.findClientByUserNameAndPassword
               (inputTextUserLogin.getValue().toString(), inputSecretUserPassword.getValue().toString());
           getSessionBean1().setUser(user);
           return "MainMenu";
        }
        catch (NonexistentEntityException ex)
        {
            ex.printStackTrace();
            panelPopupPruebaBean.setShowModalPanel(true);
            messageHeader = "Error";
            messageText = "Usuario o contraseña incorrecta";
            return null;
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            panelPopupPruebaBean.setShowModalPanel(true);
            messageHeader = "Error";
            messageText = "No ha sido posible conectarse con la base de datos";
            return null;
        }
    }

    public String btnClosePopUp_action(ActionEvent e)
    {
        panelPopupPruebaBean.setShowModalPanel(false);
        return null;
    }

    public void init()
    {
        super.init();
        try
        {
            _init();
        }
        catch (Exception e)
        {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
    }

    public void preprocess()
    {
    }

    public void prerender()
    {
    }

    public void destroy()
    {
    }
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////

    protected RequestBean1 getRequestBean1()
    {
        return (RequestBean1) getBean("RequestBean1");
    }

    protected ApplicationBean1 getApplicationBean1()
    {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    protected SessionBean1 getSessionBean1()
    {
        return (SessionBean1) getBean("SessionBean1");
    }
    
    public HtmlCommandButton getBtnClosePopUp() {
        return btnClosePopUp;
    }

    public void setBtnClosePopUp(HtmlCommandButton btnClosePopUp) {
        this.btnClosePopUp = btnClosePopUp;
    }

    public HtmlCommandButton getButtonChangePassword() {
        return buttonChangePassword;
    }

    public void setButtonChangePassword(HtmlCommandButton buttonChangePassword) {
        this.buttonChangePassword = buttonChangePassword;
    }

    public HtmlCommandButton getButtonLogin() {
        return buttonLogin;
    }

    public void setButtonLogin(HtmlCommandButton buttonLogin) {
        this.buttonLogin = buttonLogin;
    }

    public HtmlCommandLink getCommandLinkChangePassword() {
        return commandLinkChangePassword;
    }

    public void setCommandLinkChangePassword(HtmlCommandLink commandLinkChangePassword) {
        this.commandLinkChangePassword = commandLinkChangePassword;
    }

    public HtmlCommandLink getCommandLinkChangePasswordBack() {
        return commandLinkChangePasswordBack;
    }

    public void setCommandLinkChangePasswordBack(HtmlCommandLink commandLinkChangePasswordBack) {
        this.commandLinkChangePasswordBack = commandLinkChangePasswordBack;
    }

    public HtmlInputSecret getInputSecretUserPassword() {
        return inputSecretUserPassword;
    }

    public void setInputSecretUserPassword(HtmlInputSecret inputSecretUserPassword) {
        this.inputSecretUserPassword = inputSecretUserPassword;
    }

    public HtmlInputSecret getInputTextChangePasswordNew() {
        return inputTextChangePasswordNew;
    }

    public void setInputTextChangePasswordNew(HtmlInputSecret inputTextChangePasswordNew) {
        this.inputTextChangePasswordNew = inputTextChangePasswordNew;
    }

    public HtmlInputSecret getInputTextChangePasswordOld() {
        return inputTextChangePasswordOld;
    }

    public void setInputTextChangePasswordOld(HtmlInputSecret inputTextChangePasswordOld) {
        this.inputTextChangePasswordOld = inputTextChangePasswordOld;
    }

    public HtmlInputText getInputTextChangePasswordUserName() {
        return inputTextChangePasswordUserName;
    }

    public void setInputTextChangePasswordUserName(HtmlInputText inputTextChangePasswordUserName) {
        this.inputTextChangePasswordUserName = inputTextChangePasswordUserName;
    }

    public HtmlInputSecret getInputTextChangePasswordVerify() {
        return inputTextChangePasswordVerify;
    }

    public void setInputTextChangePasswordVerify(HtmlInputSecret inputTextChangePasswordVerify) {
        this.inputTextChangePasswordVerify = inputTextChangePasswordVerify;
    }

    public HtmlInputText getInputTextUserLogin() {
        return inputTextUserLogin;
    }

    public void setInputTextUserLogin(HtmlInputText inputTextUserLogin) {
        this.inputTextUserLogin = inputTextUserLogin;
    }

    public HtmlOutputLabel getLabelTitleChangePassword() {
        return labelTitleChangePassword;
    }

    public void setLabelTitleChangePassword(HtmlOutputLabel labelTitleChangePassword) {
        this.labelTitleChangePassword = labelTitleChangePassword;
    }

    public String getMessageHeader() {
        return messageHeader;
    }

    public void setMessageHeader(String messageHeader) {
        this.messageHeader = messageHeader;
    }

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    public HtmlOutputLabel getOutputLabelChangePasswordNew() {
        return outputLabelChangePasswordNew;
    }

    public void setOutputLabelChangePasswordNew(HtmlOutputLabel outputLabelChangePasswordNew) {
        this.outputLabelChangePasswordNew = outputLabelChangePasswordNew;
    }

    public HtmlOutputLabel getOutputLabelChangePasswordOld() {
        return outputLabelChangePasswordOld;
    }

    public void setOutputLabelChangePasswordOld(HtmlOutputLabel outputLabelChangePasswordOld) {
        this.outputLabelChangePasswordOld = outputLabelChangePasswordOld;
    }

    public HtmlOutputLabel getOutputLabelChangePasswordUserName() {
        return outputLabelChangePasswordUserName;
    }

    public void setOutputLabelChangePasswordUserName(HtmlOutputLabel outputLabelChangePasswordUserName) {
        this.outputLabelChangePasswordUserName = outputLabelChangePasswordUserName;
    }

    public HtmlOutputLabel getOutputLabelChangePasswordVerify() {
        return outputLabelChangePasswordVerify;
    }

    public void setOutputLabelChangePasswordVerify(HtmlOutputLabel outputLabelChangePasswordVerify) {
        this.outputLabelChangePasswordVerify = outputLabelChangePasswordVerify;
    }

    public HtmlPanelGroup getPanelChangePassword() {
        return panelChangePassword;
    }

    public void setPanelChangePassword(HtmlPanelGroup panelChangePassword) {
        this.panelChangePassword = panelChangePassword;
    }

    public HtmlPanelGroup getPanelLogin() {
        return panelLogin;
    }

    public void setPanelLogin(HtmlPanelGroup panelLogin) {
        this.panelLogin = panelLogin;
    }

    public PopupBean getPanelPopupPruebaBean() {
        return panelPopupPruebaBean;
    }

    public void setPanelPopupPruebaBean(PopupBean panelPopupPruebaBean) {
        this.panelPopupPruebaBean = panelPopupPruebaBean;
    }

    public PanelStack getPanelStackLogin() {
        return panelStackLogin;
    }

    public void setPanelStackLogin(PanelStack panelStackLogin) {
        this.panelStackLogin = panelStackLogin;
    }

    public HtmlOutputLabel getOutputLabelUserLogin() {
        return outputLabelUserLogin;
    }

    public void setOutputLabelUserLogin(HtmlOutputLabel outputLabelUserLogin) {
        this.outputLabelUserLogin = outputLabelUserLogin;
    }

    public HtmlOutputLabel getOutputLabelUserPassword() {
        return outputLabelUserPassword;
    }

    public void setOutputLabelUserPassword(HtmlOutputLabel outputLabelUserPassword) {
        this.outputLabelUserPassword = outputLabelUserPassword;
    }

}

